package cn.shadow.game.chess.CHnchess.chessman;

import cn.shadow.game.chess.CHnchess.board.Board;
import cn.shadow.game.chess.CHnchess.board.Line;
import cn.shadow.game.chess.CHnchess.board.Position;
import cn.shadow.game.chess.CHnchess.type.Camp;
import cn.shadow.game.chess.CHnchess.type.ChessType;

public class Bing extends AbstractChess implements Line {

    public Bing(Board board, Camp camp) {
        super(ChessType.BING, board, camp);
    }


    @Override
    public void addLeftPoint() {
        int row = this.getRow();
        int col = this.getCol();
        if (isPassRiver()) {
            return;
        }
        Position point = board.getPoint(row, col - 1);
        if (point != null) {
            addPoint(point);
        }

    }

    @Override
    public void addRightPoint() {
        int row = this.getRow();
        int col = this.getCol();
        if (isPassRiver()) {
            return;
        }
        Position point = getBoard().getPoint(row, col + 1);
        if (point != null) {
            addPoint(point);
        }

    }

    @Override
    public void addUpPoint() {
        int row = this.getRow();
        int col = this.getCol();
        if (this.getCamp() == Camp.BLACK) {
            Position point = getBoard().getPoint(row - 1, col);
            if (point != null) {
                addPoint(point);
            }
        }
    }

    @Override
    public void addDownPoint() {
        int row = this.getRow();
        int col = this.getCol();
        if (this.getCamp() == Camp.RED) {
            Position point = getBoard().getPoint(row + 1, col);
            if (point != null) {
                addPoint(point);
            }
        }

    }

    private void addPoint(Position point) {
        if (point.isOverRanged()) {
            return;
        }
        Chess chess = point.getChess();
        if (chess != null) {
            if (chess.getCamp() == this.getCamp()) {
                return;
            }
        }
        canSetPosition.add(point);
        canAttackPosition.add(point);
    }

    /**
     * 黑方过河的条件是:
     * 阵营是黑色,然后所在的点的行坐标小于4 row<4
     * 红色过河的条件
     *
     * @return
     */
    private boolean isPassRiver() {
        return (this.getCamp() != Camp.BLACK || this.getPosition().isDown())
                && (this.getCamp() != Camp.RED || !this.getPosition().isDown());
    }

}
